package test.com.googlecode.likesql.utils;

import junit.framework.TestCase;

import com.googlecode.likesql.utils.StringUtils;

public class StringUtilsTester extends TestCase {

	public void test_replaceFirst(){
		String pattern="? and ? is here.";
		assertEquals("John and ? is here.",StringUtils.replaceFirst(pattern, '?', "John"));
		assertEquals("null and ? is here.",StringUtils.replaceFirst(pattern, '?', null));
	}
	
	public void test_replaceFirst1(){
		String pattern="@name1 and @name2 is here.";
		assertEquals("John and @name2 is here.",StringUtils.replaceFirst(pattern, "@name1", "John"));
		assertEquals("null and @name2 is here.",StringUtils.replaceFirst(pattern, "@name1", null));
	}
	
	public void test_filterSql(){
		String input1="' or '1'='1";
		String input2="1';delete from table where '1'='1";
		String input3="'-----";
		String exp1=" or 1=1";
		String exp2="1delete from table where 1=1";
		String exp3="-";
		
		assertEquals(exp1,StringUtils.filterSql(input1));
		assertEquals(exp2,StringUtils.filterSql(input2));
		assertEquals(exp3,StringUtils.filterSql(input3));
	}
	
	public void test_replaceAll(){
		String pattern= "?, ? and ? are in the room.";
		String[] names=new String[]{"Joe","Apple","Tim"};
		String[] names1=new String[]{"Joe","Apple","Tim","Bob"};
		String[] names2=new String[]{"Joe","Apple"};
		String[] names3=new String[]{null,"Apple",""};
		
		assertEquals("Joe, Apple and Tim are in the room.",StringUtils.replaceAll(pattern, '?', names));
		assertEquals("Joe, Apple and Tim are in the room.",StringUtils.replaceAll(pattern, '?', names1));
		assertEquals("Joe, Apple and ? are in the room.",StringUtils.replaceAll(pattern, '?', names2));
		assertEquals("?, Apple and  are in the room.",StringUtils.replaceAll(pattern, '?', names3));
	}
	
	public void test_charIndexes(){
		String pattern= "?, ? and ? are in the room.";
		int[] expect=new int[]{0,3,9};
		int[] actual=StringUtils.charIndexes(pattern, '?');
		
		for(int i=0;i<expect.length;i++){
			assertEquals(expect[i],actual[i]);
		}
	}
}
